/* model */
import { 
  BaseSelectUserStoreUserCountMapDataType,
  BaseSelectUserStoreUserCountMapType,
  BaseSelectUserStoreUserListMapType, 
  BaseSelectUserUserListKeyEnum,  
  BaseSelectUserVuexPropertyEnum, 
  useBaseSelectUserStoreDepartmentData
} from '@src/component/common/BaseSelectUser/store'
import {
  SelectUserComponentUser, 
  SelectUserLabel, 
  SelectUserState 
} from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* type */
import { BaseSelectUserGetLabelListResult, BaseSelectUserGetStateListResult } from '@src/component/common/BaseSelectUser/types'
import { isEmpty } from '@src/util/type'

/** 
 * @description 选人控件 用户状态列表
*/
function useBaseSelectUserStoreStateList() {
  
  const store = useBaseSelectUserStore()
  
  const userStateList = computed(() => store.state.userStateList)
  
  const setUserStateList = (value: SelectUserState[]) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserStateList, value)
  }
  
  return {
    userStateList,
    setUserStateList
  }
}

/** 
 * @description 选人控件 人员标签列表
*/
function useBaseSelectUserStoreLabelList() {
  
  const store = useBaseSelectUserStore()
  
  const userLabelList = computed(() => store.state.userLabelList)
  
  const setUserLabelList = (value: SelectUserLabel[]) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserLabelList, value)
  }
  
  return {
    userLabelList,
    setUserLabelList
  }
}

/** 
 * @description 选人控件 人员列表
*/
function useBaseSelectUserStoreUserListMap() {
  
  const store = useBaseSelectUserStore()
  
  // 用户列表对象
  const userListMap = computed(() => store.state.userListMap)
  
  // 部门用户列表
  const departmentUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.Department]
  })
  
  // 根部门用户列表
  const departmentRootUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.DepartmentRoot]
  })
  
  // 角色用户列表
  const roleUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.Role]
  })
  
  // 服务商用户列表
  const serviceProviderUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.ServiceProvider]
  })
  
  // 项目组用户列表
  const projectTeamUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.ProjectTeam]
  })

  // 搜索更多用户列表
  const searchMoreUserList = computed(() => {
    return userListMap.value[BaseSelectUserUserListKeyEnum.SearchMore]
  })
  
  // 根部门用户列表是否为空
  const isDepartmentRootUserListEmpty = computed(() => isEmpty(departmentRootUserList.value))
  // 部门用户列表是否为空
  const isDepartmentUserListEmpty = computed(() => isEmpty(departmentUserList.value))
  // 角色用户列表是否为空
  const isRoleUserListEmpty = computed(() => isEmpty(roleUserList.value))
  // 服务商用户列表是否为空
  const isServiceProviderUserListEmpty = computed(() => isEmpty(serviceProviderUserList.value))
  // 项目组用户列表是否为空
  const isProjectTeamUserListEmpty = computed(() => isEmpty(projectTeamUserList.value))
  // 搜索更多用户列表是否为空
  const isSearchMoreUserListEmpty = computed(() => isEmpty(searchMoreUserList.value))
  
  const setUserListMap = (value: BaseSelectUserStoreUserListMapType) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMap, value)
  }
  
  const setDepartmentRootUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapDepartmentRootUserList, value)
  }
  
  const setDepartmentUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapDepartmentUserList, value)
  }
  
  const setRoleUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapRoleUserList, value)
  }
  
  const setServiceProviderUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapServiceProviderUserList, value)
  }

  const setProjectTeamUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapProjectTeamUserList, value)
  }

  const setSearchMoreUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserListMapSearchMoreUserList, value)
  }
  
  return {
    userListMap,
    
    departmentRootUserList,
    departmentUserList,
    roleUserList,
    serviceProviderUserList,
    projectTeamUserList,
    searchMoreUserList,
    
    isDepartmentRootUserListEmpty,
    isDepartmentUserListEmpty,
    isRoleUserListEmpty,
    isServiceProviderUserListEmpty,
    isProjectTeamUserListEmpty,
    isSearchMoreUserListEmpty,
    
    setUserListMap,
    
    setDepartmentUserList,
    setRoleUserList,
    setServiceProviderUserList,
    setDepartmentRootUserList,
    setProjectTeamUserList,
    setSearchMoreUserList
  }
}

/** 
 * @description 选人控件 人员数量
*/
function useBaseSelectUserStoreUserCountMap() {
  
  const store = useBaseSelectUserStore()
  
  // 用户数量对象
  const userCountMap = computed(() => store.state.userCountMap)
  
  // 根部门用户数量
  const departmentRootUserCount = computed(() => {
    return userCountMap.value[BaseSelectUserUserListKeyEnum.DepartmentRoot]
  })
  
  // 部门用户数量
  const departmentUserCount = computed(() => {
    return userCountMap.value[BaseSelectUserUserListKeyEnum.Department]
  })
  
  // 角色用户数量
  const roleUserCount = computed(() => {
    return userCountMap.value[BaseSelectUserUserListKeyEnum.Role]
  })
  
  // 服务商用户数量
  const serviceProviderUserCount = computed(() => {
    return userCountMap.value[BaseSelectUserUserListKeyEnum.ServiceProvider]
  })
  
  // 搜索更多用户数量
  const searchMoreUserCount = computed(() => {
    return userCountMap.value[BaseSelectUserUserListKeyEnum.SearchMore]
  })
  
  const setUserCountMap = (value: BaseSelectUserStoreUserCountMapType) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMap, value)
  }
  
  const setDepartmentRootUserCount = (value: BaseSelectUserStoreUserCountMapDataType = 0) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMapDepartmentRootUserCount, value)
  }
  
  const setDepartmentUserCount = (value: BaseSelectUserStoreUserCountMapDataType = 0) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMapDepartmentUserCount, value)
  }
  
  const setRoleUserCount = (value: BaseSelectUserStoreUserCountMapDataType = 0) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMapRoleUserCount, value)
  }
  
  const setServiceProviderUserCount = (value: BaseSelectUserStoreUserCountMapDataType = 0) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMapServiceProviderUserCount, value)
  }
  
  const setSearchMoreUserCount = (value: BaseSelectUserStoreUserCountMapDataType = 0) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetUserCountMapSearchMoreUserCount, value)
  }
  
  return {
    departmentUserCount,
    roleUserCount,
    serviceProviderUserCount,
    departmentRootUserCount,
    searchMoreUserCount,
    setUserCountMap,
    setDepartmentRootUserCount,
    setDepartmentUserCount,
    setRoleUserCount,
    setServiceProviderUserCount,
    setSearchMoreUserCount
  }
}

/** 
 * @description 查询选人控件的用户状态列表
*/
function useBaseSelectUserStoreFetchStateList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchStateList(): Promise<MsgModel<BaseSelectUserGetStateListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchUserStateList).then(result => {
        return result
      })
    )
  }
  
  return fetchStateList
}

/** 
 * @description 查询选人控件的人员标签列表
*/
function useBaseSelectUserStoreFetchLabelList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchLabelList(): Promise<MsgModel<BaseSelectUserGetLabelListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchUserLabelList).then(result => {
        return result
      })
    )
  }
  
  return fetchLabelList
}

export {
  useBaseSelectUserStoreStateList,
  useBaseSelectUserStoreLabelList,
  useBaseSelectUserStoreUserListMap,
  useBaseSelectUserStoreUserCountMap,
  useBaseSelectUserStoreFetchStateList,
  useBaseSelectUserStoreFetchLabelList
}